package com.bdq.search.application.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.bdq.common.entity.UserContext;
import com.bdq.common.entity.common.PageResult;
import com.bdq.common.feign.content.param.ContentSearchParam;
import com.bdq.common.feign.content.vo.ContentListVO;
import com.bdq.common.utils.UserContextUtil;
import com.bdq.search.application.model.cmd.SearchHistoryDeleteCmd;
import com.bdq.search.application.model.dto.SearchHistoryAppDTO;
import com.bdq.search.application.model.query.SearchPageQry;
import com.bdq.search.application.service.SearchQueryService;
import com.bdq.search.domain.model.dto.req.SearchHistoryAddReqDTO;
import com.bdq.search.domain.model.dto.req.SearchHistoryDeleteReqDTO;
import com.bdq.search.domain.service.MyFeignClient;
import com.bdq.search.domain.service.SearchDomainService;
import com.bdq.search.infrastructure.config.TokenInterceptor;
import com.bdq.search.infrastructure.persistence.mysql.pojo.SearchHistory;
import com.bdq.search.infrastructure.persistence.mysql.service.SearchHistoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author malei
 * @date 2024-05-26 16:40
 */
@Service
public class SearchQueryServiceImpl implements SearchQueryService {

    @Resource
    private SearchDomainService searchDomainService;

    @Resource
    private SearchHistoryService searchHistoryService;

    @Resource
    private MyFeignClient myFeignClient;

    @Override
    public PageResult<ContentListVO> searchPage(SearchPageQry qry) {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());

        // 2 删除搜索历史
        SearchHistoryDeleteReqDTO deleteReqDTO = new SearchHistoryDeleteReqDTO();
        deleteReqDTO.setUserId(userContext.getId());
        deleteReqDTO.setKeyword(qry.getKeyword());
        searchDomainService.deleteSearchHistory(deleteReqDTO);

        // 3 保存搜索历史
        SearchHistoryAddReqDTO reqDTO = new SearchHistoryAddReqDTO();
        reqDTO.setUserId(userContext.getId());
        reqDTO.setKeyword(qry.getKeyword());
        searchDomainService.saveSearchHistory(reqDTO);

        // 4 检索内容ID
        PageResult<Long> pageResult = myFeignClient.contentSearch(Convert.convert(ContentSearchParam.class, qry));

        // 5 内容
        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty(pageResult.getTotal());
        }
        List<ContentListVO> contentListVOS = myFeignClient.listContent(userContext.getId(), pageResult.getList());
        return new PageResult<>(contentListVOS, pageResult.getTotal());
    }

    @Override
    public List<SearchHistoryAppDTO> searchHistory() {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        // 2 搜索记录
        List<SearchHistory> searchHistoryList = searchHistoryService.listByUserId(userContext.getId());
        return Convert.toList(SearchHistoryAppDTO.class, searchHistoryList);
    }

    @Override
    public Boolean searchHistoryDelete(SearchHistoryDeleteCmd cmd) {
        return searchHistoryService.removeById(cmd.getSearchHistoryId());
    }

    @Override
    public Boolean searchHistoryClear() {
        // 1 获取用户信息
        UserContext userContext = UserContextUtil.getLoginUserContext(TokenInterceptor.getToken());
        return searchHistoryService.deleteByUserId(userContext.getId());
    }
}
